﻿Public Class ADDCUSTOMER
    Dim connect As CONDB = CONDB.NewConnection()
    Dim s As FILTERCLASS

    Private Sub ADDCUSTOMER_Load(sender As Object, e As EventArgs) Handles Me.Load
        SelectDatagridMascustomer()
        SearchCHANGWATTxt()
        AMPURTxt.Enabled = False
        SearchAMPUR()
    End Sub
    Private Sub SelectDatagridMascustomer()
        Dim objSelectDataMascustomer As New MASCUSTOMER
        Dim MASCUSTOMER As DataTable
        MASCUSTOMER = connect.GetTable(objSelectDataMascustomer.SelectDatagridMascustomer_)
        ADDCUSGrid.PrimaryGrid.DataSource = MASCUSTOMER
    End Sub
    Private Sub SearchCHANGWATTxt()
        Dim sql2 As String
        sql2 = "SELECT CAST(idmaschangwat AS CHAR(10)) AS `idmaschangwat`,`changwat` FROM cpm.maschangwat WHERE `STATUS` = 1"
        s = New FILTERCLASS(CHANGWATTxt, sql2, "รหัสจังหวัด,จังหวัด", "100,200", "1,1", "1,1")
        s.SetShowBorder = True
    End Sub
    Private Sub SearchAMPUR()
      
    End Sub

    Private Sub AddCus_Click(sender As Object, e As EventArgs) Handles AddCus.Click
        If NAMETxt.Text.ToString.Length > 0 And CIDTxt.Text.Length > 0 Then
            Dim objInsertData As New MASCUSTOMER
            objInsertData.NAME_ = NAMETxt.Text
            objInsertData.CID_ = CIDTxt.Text
            objInsertData.TELL_ = TELLTxt.Text
            objInsertData.IDMASCHANGWAT_ = Convert.ToString(CHANGWATTxt.Tag).Trim
            objInsertData.IDMASAMPUR_ = Convert.ToString(AMPURTxt.Tag).Trim
            objInsertData.ADDRESS_ = ADDRESSTxt.Text
            objInsertData.TAX_ = TAXTxt.Text
            objInsertData.REGISTRATION_ = REGISTRATIONTxt.Text
            objInsertData.CREDIT_ = CreditTxt.Text
            objInsertData.EMAIL_ = EMAILTxt.Text
            objInsertData.CODE_ = CODETxt.Text
            objInsertData.INSERTMASCUSTOMER()

            SelectDatagridMascustomer()

            cleardata()
        Else
            MsgBox("โปรดระบุข้อมูลให้ครบถ้วน", MsgBoxStyle.Critical)
        End If
    End Sub

    Private Sub EditCus_Click(sender As Object, e As EventArgs) Handles EditCus.Click
        If CUSTOMERIDTxt.Text.ToString.Length > 0 Then
            Dim editStat As Integer
            editStat = MsgBox("ท่านต้องการแก้ไขข้อมูลส่วนดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)
            If editStat = 1 Then
                Dim objeditData As New MASCUSTOMER
                objeditData.IDMASCUSTOMER_ = CUSTOMERIDTxt.Text
                objeditData.NAME_ = NAMETxt.Text
                objeditData.CID_ = CIDTxt.Text
                objeditData.TELL_ = TELLTxt.Text
                objeditData.IDMASAMPUR_ = Convert.ToString(AMPURTxt.Tag).Trim
                objeditData.IDMASCHANGWAT_ = Convert.ToString(CHANGWATTxt.Tag).Trim
                objeditData.ADDRESS_ = ADDRESSTxt.Text
                objeditData.EMAIL_ = EMAILTxt.Text
                objeditData.TAX_ = TAXTxt.Text
                objeditData.REGISTRATION_ = REGISTRATIONTxt.Text
                objeditData.CREDIT_ = CreditTxt.Text
                objeditData.UPDATEMASCUSTOMER()

                SelectDatagridMascustomer()

                cleardata()
            End If
        Else
            MsgBox("โปรดระบุข้อมูลให้ครบถ้วน", MsgBoxStyle.Critical)
        End If
        AddCus.Enabled = True
    End Sub
    Private Sub CHANGWATTxt_TextChanged(sender As Object, e As EventArgs) Handles CHANGWATTxt.TextChanged
        If Convert.ToString(CHANGWATTxt.Tag).Trim = "" Then
            AMPURTxt.Enabled = False
            AMPURTxt.Text = ""
            Return
        Else
            AMPURTxt.Enabled = True

        End If
        Dim sql As String
        sql = "SELECT CAST(`idmasampur` AS NCHAR(3)) AS `idmasampur`,`ampur` "
        sql += " FROM cpm.masampur "
        sql += " WHERE `idmaschangwat` = '" & Convert.ToString(CHANGWATTxt.Tag).Trim & "'; "
        s = New FILTERCLASS(AMPURTxt, sql, "รหัสอำเภอ,อำเภอ", "100,200", "1,1", "1,1")
        s.SetShowBorder = True
    End Sub

    Private Sub ADDCUSGrid_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles ADDCUSGrid.CellClick
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow
        If grid.Cells("รหัส").Value() IsNot DBNull.Value Then
            CUSTOMERIDTxt.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัส").Value()
        Else
            CUSTOMERIDTxt.Text = ""
        End If
        If grid.Cells("ชื่อลูกค้า").Value() IsNot DBNull.Value Then
            NAMETxt.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อลูกค้า").Value()
        Else
            NAMETxt.Text = ""
        End If
        If grid.Cells("เลขบัตรประชาชน").Value() IsNot DBNull.Value Then
            CIDTxt.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("เลขบัตรประชาชน").Value()
        Else
            CIDTxt.Text = ""
        End If
        If grid.Cells("idmaschangwat").Value() IsNot DBNull.Value Then
            CHANGWATTxt.Tag = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("idmaschangwat").Value()
        Else
            CHANGWATTxt.Tag = ""
        End If
        If grid.Cells("จังหวัด").Value() IsNot DBNull.Value Then
            CHANGWATTxt.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("จังหวัด").Value()
        Else
            CHANGWATTxt.Text = ""
        End If
        If grid.Cells("idmasampur").Value() IsNot DBNull.Value Then
            AMPURTxt.Tag = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("idmasampur").Value()
        Else
            AMPURTxt.Tag = ""
        End If
        If grid.Cells("อำเภอ").Value() IsNot DBNull.Value Then
            AMPURTxt.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("อำเภอ").Value()
        Else
            AMPURTxt.Text = ""
        End If
        If grid.Cells("เบอร์").Value() IsNot DBNull.Value Then
            TELLTxt.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("เบอร์").Value()
        Else
            TELLTxt.Text = ""
        End If
        If grid.Cells("ที่อยู่").Value() IsNot DBNull.Value Then
            ADDRESSTxt.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ที่อยู่").Value()
        Else
            ADDRESSTxt.Text = ""
        End If
        If grid.Cells("email").Value() IsNot DBNull.Value Then
            EMAILTxt.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("email").Value()
        Else
            EMAILTxt.Text = ""
        End If
        If grid.Cells("tax").Value() IsNot DBNull.Value Then
            TAXTxt.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("tax").Value()
        Else
            TAXTxt.Text = ""
        End If
        If grid.Cells("credit").Value() IsNot DBNull.Value Then
            CreditTxt.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("credit").Value()
        Else
            CreditTxt.Text = ""
        End If
        If grid.Cells("CODE").Value() IsNot DBNull.Value Then
            CODETxt.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("CODE").Value()
        Else
            CODETxt.Text = ""
        End If
        If grid.Cells("registration").Value() IsNot DBNull.Value Then
            REGISTRATIONTxt.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("registration").Value()
        Else
            REGISTRATIONTxt.Text = ""
        End If
        connect = CONDB.NewConnection()
        If e.GridCell.GridColumn.Name = "สถานะการเปิดใช้งาน" Then
            Try
                Dim Sql As String
                connect.BeginTrans()
                Sql = "UPDATE cpm.mascustomer SET "
                If Convert.ToBoolean(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value()) = True Then
                    Sql += "`STATUS` = 1 "
                Else
                    Sql += "`STATUS` = 0 "
                End If
                Sql += "WHERE `idmascustomer` = '" & CUSTOMERIDTxt.Text & "' ;"
                connect.ExecuteNonQuery(Sql)
                connect.CommitTrans()
            Catch ex As Exception
                connect.RollbackTrans()
                MsgBox(ex.ToString)
            End Try
        End If
        AddCus.Enabled = False
    End Sub
    Private Sub cleardata()
        CUSTOMERIDTxt.Text = ""
        NAMETxt.Text = ""
        CIDTxt.Text = ""
        ADDRESSTxt.Text = ""
        EMAILTxt.Text = ""
        TELLTxt.Text = ""
        CHANGWATTxt.Text = ""
        AMPURTxt.Text = ""
        TAXTxt.Text = ""
        REGISTRATIONTxt.Text = ""
        SelectDatagridMascustomer()
        CreditTxt.Text = ""
    End Sub

    Private Sub ButtonX1_Click(sender As Object, e As EventArgs) Handles ButtonX1.Click
        cleardata()
    End Sub
End Class